package com.lgy.controller;

import com.github.pagehelper.PageInfo;
import com.lgy.pojo.*;
import com.lgy.service.ICarBrandService;
import com.lgy.service.ICarOrderService;
import com.lgy.service.ICarService;
import com.lgy.service.ICarseriesService;
import com.lgy.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private ICarBrandService carBrandService;
    @Autowired
    private ICarseriesService carseriesService;
    @Autowired
    private ICarService carService;
    @Autowired
    private ICarOrderService carOrderService;
    //getAllCarBrand.do
    @RequestMapping("/getAllCarBrand.do")
    @ResponseBody
    private ResultData<CarBrand> getAllCarBrand(){
        List<CarBrand> carBrandList = carBrandService.findAll();
        if(carBrandList!=null){
            return ResultData.ok(carBrandList);
        }
        return ResultData.error("没有数据");
    }

    //getAllCarSeries.do
    @RequestMapping("/getAllCarSeries.do")
    @ResponseBody
    public ResultData<Carseries> getAllCarSeries(@RequestParam("brandId")Integer brandId){
        List<Carseries> carseriesList = carseriesService.findCarSeriesAll(brandId);
        if(carseriesList!=null){
            return ResultData.ok(carseriesList);
        }
        return ResultData.error("查无数据");
    }

    //getAllCarSeries.do
    @RequestMapping("/getAllCar.do")
    @ResponseBody
    public ResultData<Car> getAllCar(@RequestParam("seriesId")Integer seriesId){
        List<Car> carList = carService.findCarAllById(seriesId);
        if(carList!=null){
            return ResultData.ok(carList);
        }
        return ResultData.error("查无数据");
    }
    //getCarPriceById.do
    @RequestMapping("/getCarPriceById.do")
    @ResponseBody
    public ResultData<Car> getCarPriceById(@RequestParam("carId")Integer carId){
        Car car = carService.findCarPriceById(carId);
        if(car!=null){
            return ResultData.ok(car);
        }
        return ResultData.error("查无数据");
    }
    ///order/insertOrder.do
    @RequestMapping(value = "/insertOrder.do",method = RequestMethod.POST)
    public String insertOrder(CarOrder carOrder, Double carPrice){
        carOrder.setOrderTotalPrice(new BigDecimal(carPrice*carOrder.getOrderNum()));
        int row = carOrderService.insertOrder(carOrder);
        return "redirect:/repertory/getAllRepertory.do";
    }

    //payment.do
    @RequestMapping("/getOrderList.do")
    public String payment(Model model, HttpSession session,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        Employee emp = (Employee) session.getAttribute("emp");
        PageInfo<CarOrder> pageInfo = carOrderService.findOrderListByCompanyId(emp.getCompanyid(),page,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "AdminPaymentList";
    }

    //order/updateOrderById.do
    @RequestMapping("/updateOrderById.do")
    public String updateOrderById(HttpSession session, Integer orderId, Integer carId, Integer orderNum){
        Employee emp = (Employee) session.getAttribute("emp");
        int row = carOrderService.updateOrderType(orderId,carId,emp.getCompanyid(),orderNum);
        if(row<=0){
            session.setAttribute("error","审核未通过");
        }else {
            session.removeAttribute("error");
        }
        return "redirect:/order/getOrderList.do";
    }

}
